package com.yoyo.itravel.dao;

import com.yoyo.itravel.bean.SightComment;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;

/**
 * @Description 景点评论持久层接口
 * @Author HZX
 * @Version 1.0
 * @Date 2020/6/26 11:29
 */
@Repository
public interface ISightCommentDao {

    /**
     * @Description 插入评论数据
     *
     * @param sightComment
     * @return int
     */
    @Insert("insert into tb_sight_comment(sightCommentId,sightId,userId,content,likeNum,createTime,starNum) " +
            "values(#{sightComment.sightCommentId},#{sightComment.sightId},#{sightComment.userId},#{sightComment.content},#{sightComment.likeNum},#{sightComment.createTime},#{sightComment.starNum})")
    int insert(SightComment sightComment) throws SQLException;

    /**
     * @Description 通过评论id删除景点评论数据
     *
     * @param commentId
     * @return int
     */
    @Delete("delete from tb_sight_comment where sightCommentId=#{commentId}")
    int deleteByCommentId(String commentId) throws SQLException;

    /**
     * @Description 通过评论id查询景点评论数据
     *
     * @param commentId
     * @return SightCollect
     */
    @Select("select * from tb_sight_comment where sightCommentId=#{commentId}")
    SightComment selectByCommentId(String commentId) throws SQLException;

    /**
     * @Description 通过用户id查询该用户发表的所有的对景点的评论
     *
     * @param userId
     * @return java.util.List<cn.hut.travel.bean.Sight>
     */
    @Select("select * from tb_sight_comment where userId=#{userId}")
    List<SightComment> selectAllByUserId(String userId) throws SQLException;

    /**
     * @Description 通过景点id获得该景点的所有评论
     *
     * @param sightId
     * @return java.util.List<cn.hut.travel.bean.SightComment>
     */
    @Select({"<script> select * from tb_sight_comment where sightId=#{sightId} " +
            "<when test='null!=#{starNum}'> and starNum=#{starNum}</when></script>"})
    List<SightComment> selectAllBySightIdAndStar(@Param("sightId") String sightId, @Param("starNum") Integer starNum) throws SQLException;

    /**
     * @Description 通过评论id修改点赞数
     *
     * @param commentId
     * @param isAdd
     * @return boolean
     */
    @Update({"<script> update tb_sight_comment set " +
            "<when test='true==#{isAdd}'> likeNum=likeNum+1 </when>   " +
            "<when test='true!=#{isAdd}'> likeNum=likeNum-1 </when>  " +
            "where sightCommentId=#{commentId} </script>"})
    int updateLikeNum(@Param("commentId") String commentId, @Param("isAdd") boolean isAdd) throws SQLException;
}
